import React, { useEffect, useState } from 'react'
import Home from '../../views/sandbox/home/Home'
import RoleList from '../../views/sandbox/right-manage/RoleList'
import UserList from '../../views/sandbox/user-manage/UserList'
import RightList from '../../views/sandbox/right-manage/RightList'
import { Redirect, Route, Switch } from 'react-router-dom'
import NoPermission from '../../views/nopermission/NoPermission'
import NewsAdd from '../../views/sandbox/news-manage/NewsAdd'
import NewsDraft from '../../views/sandbox/news-manage/NewsDraft'
import NewsCategory from '../../views/sandbox/news-manage/NewsCategory'
import Audit from '../../views/sandbox/audit-manage/Audit'
import AuditList from '../../views/sandbox/audit-manage/AuditList'
import Published from '../../views/sandbox/publish-manage/Published'
import Sunset from '../../views/sandbox/publish-manage/Sunset'
import Unpublished from '../../views/sandbox/publish-manage/Unpublished'
import NewsPreview from '../../views/sandbox/news-manage/NewsPreview'
import NewsUpdate from '../../views/sandbox/news-manage/NewsUpdate'
import axios from 'axios'
import { Spin } from 'antd'
import { connect } from 'react-redux'
// 路由列表
const LocalRouterMap = {
    "/home": Home,
    "/user-manage/list": UserList,
    "/right-manage/role/list": RoleList,
    "/right-manage/right/list": RightList,
    "/news-manage/add": NewsAdd,
    "/news-manage/draft": NewsDraft,
    "/news-manage/category": NewsCategory,
    "/news-manage/preview/:id": NewsPreview,
    "/audit-manage/audit": Audit,
    "/audit-manage/list": AuditList,
    "/publish-manage/unpublished": Unpublished,
    "/publish-manage/published": Published,
    "/publish-manage/sunset": Sunset,
    "/news-manage/update/:id": NewsUpdate
}
function NewsRouter(props) {
    const [backRouteList, setBackRouteList] = useState([])
    // 合并两个接口的数据
    useEffect(() => {
        Promise.all([
            axios.get("/rights"),
            axios.get("/children"),
        ]).then(res => {
            // console.log(res);
            setBackRouteList([...res[0].data, ...res[1].data])
            // console.log([...res[0].data,...res[1].data]);
        })
    }, [])

    // 判断任务列表配置permission是否有权限
    const checkRoute = (item) => {
        return LocalRouterMap[item.key] && (item.pagepermisson || item.routepermisson)
    }
    // 判断当前用户是否有这个权限
    const { role: { rights } } = JSON.parse(localStorage.getItem("token"))
    const checkUserPermission = (item) => {
        return rights.includes(item.key)
    }
    return (
        <div>
            <Spin size="large" spinning={props.isLoading}>
                <Switch>
                    {
                        // 筛选组件
                        backRouteList.map(item => {
                            if (checkRoute(item) && checkUserPermission(item)) {
                                return (
                                    <Route path={item.key} key={item.key}
                                        component={LocalRouterMap[item.key]} exact></Route>
                                )
                            }
                            return null
                        })
                    }
                    <Redirect from="/" to="/home" exact />
                    {
                        backRouteList.length > 0 && <Route path="*" component={NoPermission}></Route>
                    }
                </Switch>

            </Spin>
        </div>
    )
}

const mapStateToProps=({LoadingReducer:{isLoading}})=>{
    // console.log(state);
    return {
        isLoading,
    }
}
// const mapDispatchToProps={
//     changeCollapesd(){
//         return {
//             type:"change_Loading",
//         }
//     }
// }

export default connect(mapStateToProps)(NewsRouter)